package com.noahplat.device.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.noahplat.common.utils.NoahEnum;
import com.noahplat.common.utils.RedisUtils;
import com.noahplat.common.utils.Result;
import com.noahplat.common.utils.StatusCode;
import com.noahplat.device.mapper.DeviceMapper;
import com.noahplat.device.mapper.TypeMapper;
import com.noahplat.device.pojo.Device;
import com.noahplat.device.pojo.Type;
import com.noahplat.device.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: TypeServiceImpl
 * @Description: 设备类型实现类
 * @Author 李景星
 * @Date 2021/3/9
 * @Version 1.0
 */
@Service
public class TypeServiceImpl implements TypeService {

    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 获取所有设备类型
     * @return
     */
    @Override
    public Result getType() {
        /*先redis,再数据库*/
        List<Type> list = new ArrayList<>();
        if(redisUtils.hasKey(NoahEnum.TYPEDIR.getCode()+NoahEnum.TYPEKEY.getCode())){
            list = (List<Type>)(List)redisUtils.lGet(NoahEnum.TYPEDIR.getCode() + NoahEnum.TYPEKEY.getCode(), 0, -1);
        }else {
            list = typeMapper.selectAll();
            for (Type type : list) {
                redisUtils.lSet(NoahEnum.TYPEDIR.getCode()+NoahEnum.TYPEKEY.getCode(), type);
            }
        }
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 通过ID查询设备类型
     * @param id
     * @return
     */
    public Result getById(Long id){
        List<Type> list = new ArrayList<>();
        if(redisUtils.hasKey(NoahEnum.TYPEDIR.getCode()+NoahEnum.TYPEKEY.getCode())){
            list = (List<Type>)(List)redisUtils.lGet(NoahEnum.TYPEDIR.getCode() + NoahEnum.TYPEKEY.getCode(), 0, -1);
        }else {
            list = typeMapper.selectAll();
            for (Type type : list) {
                redisUtils.lSet(NoahEnum.TYPEDIR.getCode()+NoahEnum.TYPEKEY.getCode(), type);
            }
        }
        for(Type type : list){
            if(type.getId() == id){
                return new Result(true,StatusCode.OK,"查询成功",type);
            }
        }
        return new Result(false,StatusCode.ERROR,"查询失败");
    }

    /**
     * 分页查询设备类型
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Result selectType(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Type> list = typeMapper.selectAll();
        if(ObjectUtils.isEmpty(list)){
            return new Result(false,StatusCode.ERROR,"暂无数据");
        }
        PageInfo<Type> pageInfo = new PageInfo<>(list);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /**
     * 添加设备类型
     * @param type
     * @return
     */
    @Transactional
    @Override
    public Result addType(Type type){
        type.setCreateTime(new Date());
        int res = typeMapper.insert(type);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
        boolean inres = redisUtils.lSet(NoahEnum.TYPEDIR.getCode()+NoahEnum.TYPEKEY.getCode(), type);
        if(!inres){
            return new Result(false,StatusCode.ERROR,"添加内存失败");
        }
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 修改设备类型
     * @param type
     * @return
     */
    @Transactional
    @Override
    public Result updateType(Type type){
        int res = typeMapper.updateByPrimaryKeySelective(type);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
        if(redisUtils.hasKey(NoahEnum.TYPEDIR.getCode() + NoahEnum.TYPEKEY.getCode())){//从redis中获取数据
            List<Object> list = redisUtils.lGet(NoahEnum.TYPEDIR.getCode() + NoahEnum.TYPEKEY.getCode(), 0, -1);
            for(Object object: list){
                Type type1 = (Type) object;
                if(type1.getId() == type.getId()){
                    long delres = redisUtils.lRemove(NoahEnum.TYPEDIR.getCode() + NoahEnum.TYPEKEY.getCode(),1,object);
                    if(delres == 0){
                        return new Result(false,StatusCode.ERROR,"删除内存出错");
                    }
                    boolean inres = redisUtils.lSet(NoahEnum.TYPEDIR.getCode() + NoahEnum.TYPEKEY.getCode(),type);
                    if(!inres){
                        return new Result(false,StatusCode.ERROR,"插入内存出错");
                    }
                    break;
                }
            }
        }else {
            List<Type> list = typeMapper.selectAll();
            for (Type type1 : list) {
                if(type.getId() == type1.getId()){
                    redisUtils.lSet(NoahEnum.TYPEDIR.getCode() + NoahEnum.TYPEKEY.getCode(), type);
                    continue;
                }
                redisUtils.lSet(NoahEnum.TYPEDIR.getCode() + NoahEnum.TYPEKEY.getCode(), type1);
            }
        }
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除设备类型
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Result delType(Long id){
        Example example = new Example(Type.class);
        example.createCriteria().andEqualTo("typeid",id);
        List<Device> list = deviceMapper.selectByExample(example);
        if(!ObjectUtils.isEmpty(list)){
            return new Result(false,StatusCode.ERROR,"正在使用，禁止删除",list);
        }
        int res = typeMapper.deleteByPrimaryKey(id);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"删除失败");
        }
        if(redisUtils.hasKey(NoahEnum.TYPEDIR.getCode() + NoahEnum.TYPEKEY.getCode())){//从redis中获取数据
            List<Object> list1 = redisUtils.lGet(NoahEnum.TYPEDIR.getCode() + NoahEnum.TYPEKEY.getCode(), 0, -1);
            for(Object object: list1){
                Type type = (Type) object;
                if(type.getId() == id){
                    long delres = redisUtils.lRemove(NoahEnum.TYPEDIR.getCode() + NoahEnum.TYPEKEY.getCode(),1,object);
                    if(delres == 0){
                        return new Result(false,StatusCode.ERROR,"删除内存出错");
                    }
                    break;
                }
            }
        }else {
            List<Type> list1 = typeMapper.selectAll();
            for (Type type : list1) {
                if(type.getId() == id){
                    continue;
                }
                redisUtils.lSet(NoahEnum.TYPEDIR.getCode() + NoahEnum.TYPEKEY.getCode(), type);
            }
        }
        return new Result(true,StatusCode.OK,"删除成功");
    }
}
